package com.ux.sns
{
	import com.ux.base.BUrlLoader;
	import com.ux.event.YouTubeEvent;
	import com.ux.util.StringUtils;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	
	[Event(name="xmlSearchSetComplete", type="com.ux.event.YouTubeEvent")]
	public class YouTubeList extends EventDispatcher
	{
		public var list : Vector.<ListVO> = new Vector.<ListVO>();
		private var searchMediaByUrl : String = "http://gdata.youtube.com/feeds/api/videos/-/"
		private var resultXml : XML;
			
		public function YouTubeList(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public function searchByUrl( word:String ):void
		{
			var url : String = encodeURI( searchMediaByUrl + word );
			var urlloader :BUrlLoader = new BUrlLoader( url, function():void
			{
				resultXml = XML(urlloader.loadData);
				listSetting();
			});
		}
		
		
		private function listSetting():void
		{
			var ns : Namespace = resultXml.namespace();
			
			var i : int = 0;
			for each( var nsEntry : XML in  resultXml.ns::entry )
			{
				var mediaNs : Namespace = nsEntry.namespace("media");
//				trace(" ==> youTubePlayerApiTest listSetting : " + group );
//				trace( entry.mediaNs::group.mediaNs::content.@type.con );
				
				
				for each( var content : XML in nsEntry.mediaNs::group.mediaNs::content )
				{
					
//					trace(" ==> youTubePlayerApiTest listSetting : " + i, content.@type);
					
					if( content.@type.contains( "application/x-shockwave-flash" ) )
					{
//						trace(" ==> youTubePlayerApiTest listSetting : " + i, content.@url);
						i++;
						
						var vo : ListVO = new ListVO();
						vo.category = nsEntry.mediaNs::group.mediaNs::category.@label;
						vo.duration = content.@duration;
						vo.movieUrl = content.@url;
						vo.title = nsEntry.ns::title;
						
						vo.thumbImgUrl_0 = nsEntry.mediaNs::group.mediaNs::thumbnail[0].@url;
						vo.thumbImgUrl_1 = nsEntry.mediaNs::group.mediaNs::thumbnail[1].@url;
						vo.thumbImgUrl_2 = nsEntry.mediaNs::group.mediaNs::thumbnail[2].@url;
						vo.thumbImgUrl_3 = nsEntry.mediaNs::group.mediaNs::thumbnail[3].@url;
						var _keyword : String = nsEntry.mediaNs::group.mediaNs::keywords;
						vo.keyword = _keyword.split(",");
						
						list.push( vo );
						
//						trace(" ==> youTubePlayerApiTest listSetting : " + nsEntry.mediaNs::group.mediaNs::thumbnail[0].@url );
					}
				}
			}
			
//			trace( "urlList.length : " + urlList.length );
//			trace( "thumList.length : " + thumList.length );
			
			
			dispatchEvent( new YouTubeEvent( YouTubeEvent.XML_SEARCH_SET_COMPLETE ) );
		}
		
		
	}
}